//
//  SYStatusCell.swift
//  SingWeibo
//
//  Created by shiying on 15/12/20.
//  Copyright © 2015年 shiying. All rights reserved.
//

import UIKit

protocol SYStatusCellDelegate: NSObjectProtocol {

    func forwradOnTheClick(status: SYStatus)
}


let SYStatusCellMargin:CGFloat = 8

class SYStatusCell: UITableViewCell {
    /// pictureView 的宽高约束
    var pictureViewWidthCon: NSLayoutConstraint?
    var pictureViewHeightCon: NSLayoutConstraint?
    
    var status: SYStatus? {
        didSet {
            topView.status = status // cell 需要显示模型的内容
            
            contentLabel.attributedText = status?.attrText //正文
            
            pictureView.status = status // 给pictureView传模型
            let size = pictureView.calcViewSize() // 得到pictureView计算好的size
            
            pictureViewWidthCon?.constant = size.width
            pictureViewHeightCon?.constant = size.height
        }
    }
    
    weak var delegate: SYStatusCellDelegate?

    //MARK: - Cell自己计算内容
    func rowHeiht(status: SYStatus) -> CGFloat {
        // 设置Cell 里面的内容，根据cell里面的内容，自己根据内容设置
        self.status = status
        
        // 更新内容，根据子控件的大小，来重新布置
        layoutIfNeeded()
        
        // 获取bottomView 最大的Y值
        let MaxY = CGRectGetMaxY(bottomView.frame)
        return MaxY
    }

    override init(style: UITableViewCellStyle, reuseIdentifier: String?) {
        super.init(style: style, reuseIdentifier: reuseIdentifier)
        prepareUI()
    }
    
    /// 准备UI
    func prepareUI() {
        contentView.addSubview(topView)
        contentView.addSubview(contentLabel)
        contentView.addSubview(bottomView)
        contentView.addSubview(pictureView)
        
        //MARK: - 约束
        topView.ff_AlignInner(type: ff_AlignType.TopLeft, referView: contentView, size: CGSize(width: UIScreen.mainScreen().bounds.width, height: 53))
        contentLabel.ff_AlignVertical(type: ff_AlignType.BottomLeft, referView: topView, size: nil, offset: CGPoint(x: SYStatusCellMargin, y: SYStatusCellMargin))
        contentView.addConstraint(NSLayoutConstraint(item: contentLabel, attribute: NSLayoutAttribute.Width, relatedBy: NSLayoutRelation.Equal, toItem: nil, attribute: NSLayoutAttribute.NotAnAttribute, multiplier: 1, constant: UIScreen.mainScreen().bounds.width - 2 * SYStatusCellMargin))
        
        /// 设置pictureView 的约束
//        let cons = pictureView.ff_AlignVertical(type: ff_AlignType.BottomLeft, referView: contentLabel, size: CGSize(width: 290, height: 290), offset: CGPoint(x: 0, y: 8))
        
//        pictureViewWidthCon = pictureView.ff_Constraint(cons, attribute: NSLayoutAttribute.Width)
//        pictureViewHeightCon = pictureView.ff_Constraint(cons, attribute: NSLayoutAttribute.Height)
        
        /// 下面按钮的约束
        bottomView.ff_AlignVertical(type: ff_AlignType.BottomLeft, referView: pictureView, size: CGSize(width: UIScreen.mainScreen().bounds.width, height: 44),offset:CGPoint(x: -8, y: 8))
        
//        contentView.addConstraint(NSLayoutConstraint(item: contentView, attribute: NSLayoutAttribute.Bottom, relatedBy: NSLayoutRelation.Equal, toItem: bottomView, attribute: NSLayoutAttribute.Bottom, multiplier: 1, constant: 0))

    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    // MARK: - 懒加载
    lazy var topView: SYStatusTopView = SYStatusTopView()
    
    lazy var contentLabel: UILabel = {
        let label = UILabel()
        
        label.font = UIFont.systemFontOfSize(14)
        label.textColor = UIColor.darkGrayColor()
        label.numberOfLines = 0
        
        return label
    }()
    
    lazy var bottomView: SYStatusBottomView = SYStatusBottomView()
    
    lazy var pictureView: SYStatusPictureView = SYStatusPictureView()
    
    

}
